package com.axend.aerosense.common.connect.ble;

import com.axend.aerosense.common.connect.bean.BleMsgEntity;
import com.axend.aerosense.common.connect.bean.BleMsgTag;
import com.axend.aerosense.common.connect.bean.BleNotifyEntity;
import com.axend.aerosense.common.connect.impl.IBleMsgProcessor;
import com.blankj.utilcode.util.LogUtils;

public abstract class AbstractBleMsgProcessor implements IBleMsgProcessor {

    /** 获取当前处理器的Tag */
    protected abstract BleMsgTag getMsgTag();

    @Override
    public byte[] write(BleMsgEntity bleMsgEntity) {
        if (null != bleMsgEntity) {
            Object body = bleMsgEntity.getBody();
            if (null != body) {
                if (body instanceof String) {
                    return body.toString().getBytes();
                } else if (body instanceof byte[]) {
                    return (byte[]) body;
                }
            }
        }
        LogUtils.w(bleMsgEntity);
        return new byte[0];
    }

    @Override
    public BleNotifyEntity processor(BleNotifyEntity notifyEntity) {
        notifyEntity.setTag(getMsgTag());
        return notifyEntity;
    }

    /** 判空 */
    protected boolean isNull(byte[] data){
        return null == data || data.length == 0;
    }
}
